/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024.
 * Description: reclaim memory requested by squashfs
 * Author: Pang LiYuan
 * Create: 2024-08-19
 */

#ifndef _RTOS_SHRINK_H
#define _RTOS_SHRINK_H

/* rtos_shrink.c */
extern struct list_head squashfs_infos;
extern struct mutex squashfs_infos_lock;
/* Be advised, cache_cnt include the number of "data cache" "fragment cache" and stream */
extern atomic_long_t squashfs_shrink_cnt;
extern int less_frag;

extern int squashfs_cache_refill_entry(struct squashfs_cache *cache,
								struct squashfs_cache_entry *entry);
extern void squashfs_cache_increase_entry(struct squashfs_cache *cache);
extern int squashfs_shrink_init(void);
extern void squashfs_shrink_exit(void);

static inline void squashfs_add_infos(struct squashfs_sb_info *sbi)
{
	mutex_lock(&squashfs_infos_lock);
	list_add_tail(&sbi->infos_list, &squashfs_infos);
	mutex_unlock(&squashfs_infos_lock);
}

static inline void squashfs_del_infos(struct squashfs_sb_info *sbi)
{
	mutex_lock(&squashfs_infos_lock);
	list_del(&sbi->infos_list);
	mutex_unlock(&squashfs_infos_lock);
}

/* call under cache->lock */
static inline void squashfs_cache_miss(struct squashfs_cache *cache)
{
	/*
	 * After we take all cache back, cache->miss will be set to zero
	 * so when the first data access after that will increase entry
	 * immediately.
	 */
	if (cache->valid_cnt >= cache->entries || (cache->miss++ % cache->entries) != 0)
		return;

	squashfs_cache_increase_entry(cache);
}
#endif
